package com.esun.payment_api.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.esun.common.api.CommonResult;
import com.esun.common.constant.Constants;
import com.esun.common.utils.BlankUtils;
import com.esun.common.utils.GetPostUntil;
import com.esun.payment_api.bean.AttachOpenIdBean;
import com.esun.payment_api.domain.XcxInfo;
import com.esun.payment_api.service.LogService;
import com.esun.payment_api.service.XcxInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/xcx")
public class XcxInfoController {

    @Resource
    XcxInfoService xcxInfoService;

    @Resource
    LogService logService;

    @ApiOperation("小程序-获取小程序app信息")
    @RequestMapping("/getApp")
    public CommonResult getApp() {
        try {
            XcxInfo app = xcxInfoService.getApp();
            return CommonResult.success(app, "获取app成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("小程序-获取小程序app信息")
    @RequestMapping("/getAccessToken")
    public CommonResult getAccessToken(Integer flowId) {
        try {
            String message = xcxInfoService.sendWxMessage(flowId);
            return CommonResult.success(message, "获取access_token成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }


    /**
     * 获取openid
     * @param
     * @param
     * @return
     */
    @CrossOrigin
    @GetMapping("/getOpenId")
    @ResponseBody
    public String getOpenId(String code, HttpServletRequest request, HttpServletResponse response){
        String appid= Constants.APP_ID;
        String secret = Constants.APP_SECRET;
        response.setHeader("Access-Control-Allow-Origin", "*");
        /*星号表示所有的域都可以接受，*/
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        String wxLoginUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
        String param = "appid="+appid+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
        String jsonString = GetPostUntil.sendGet(wxLoginUrl, param);
        JSONObject json = JSONObject.parseObject(jsonString);
        String openid = json.getString("openid");
        System.out.println("###############"+openid);
        return openid;
    }

    @CrossOrigin
    @ApiOperation("小程序-绑定微信用户")
    @RequestMapping("/attachOpenId")
    public CommonResult attachOpenId(AttachOpenIdBean attachOpenIdBean) {
        logService.writeCustomLog("测试绑定","接口层接收attachOpenId","测试");
        try {
            CommonResult commonResult = xcxInfoService.attachOpenId(attachOpenIdBean);
            logService.writeOperationLog("测试绑定", commonResult);
            return commonResult;
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("测试")
    @RequestMapping("/test")
    public CommonResult test(Integer flowId) {
        try {
            CommonResult commonResult = xcxInfoService.test(flowId);
            return commonResult;
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 小程序获取openid
     */
    @CrossOrigin
    @GetMapping("/getXcxOpenId")
    @ResponseBody
    public String getXcxOpenId(String code, HttpServletRequest request, HttpServletResponse response){
        String appid= Constants.XCX_APP_ID;
        String secret = Constants.XCX_APP_SECRET;
        response.setHeader("Access-Control-Allow-Origin", "*");
        /*星号表示所有的域都可以接受，*/
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        String wxLoginUrl = "https://api.weixin.qq.com/sns/jscode2session";
        String param = "appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
        String jsonString = GetPostUntil.sendGet(wxLoginUrl, param);
        System.out.println(jsonString);
        JSONObject json = JSONObject.parseObject(jsonString);
        String openid = json.getString("openid");
        System.out.println("###############"+openid);
        return jsonString;
    }


}
